package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.StopCarAdapter;
import com.example.smartcity.ui.Bean.StopClassBean;
import com.example.smartcity.ui.Bean.StopItem;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class stopCar extends AppCompatActivity {

    private String TAG = "TAG";
    private ListView listView;
    private TextView TV_SeeHistory;
    private TextView TV_seeMore;
    Gson gson;
    ArrayList<StopItem> arrayList = new ArrayList<>();
    ArrayList<Integer> idList = new ArrayList<>();
    private int index = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_car);
        listView = findViewById(R.id.listView);
        TV_SeeHistory = findViewById(R.id.seeHistory);
        TV_seeMore = findViewById(R.id.TV_More);
        initView();
        seeStopMessage();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/parklot/list?pageNum=1&pageSize="+index;
        OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                gson = new Gson();
                String json = response.body().string();
                ArrayList<StopClassBean.RowsBean> tempList = new ArrayList<>();
                StopClassBean bean = gson.fromJson(json,StopClassBean.class);
                if (bean.getCode()==200){
                    List<StopClassBean.RowsBean> list = bean.getRows();
                    if (list!=null){
                        tempList.addAll(list);
                    }
                    for (int i = 0; i < tempList.size(); i++) {
                        StopItem stopClassBean = new StopItem(tempList.get(i).getParkName(),tempList.get(i).getVacancy(),tempList.get(i).getDistance()
                        ,tempList.get(i).getAddress(),tempList.get(i).getRates());
                        arrayList.add(stopClassBean);
                        idList.add(tempList.get(i).getId());
                    }
                }
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        listView.setAdapter(new StopCarAdapter(getBaseContext(),R.layout.stopitemlayout,arrayList));
                    }
                });
            }
        });
        TV_SeeHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(stopCar.this,StopHistory.class));
            }
        });

    }
    public void SeeMore(View view){
         String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/parklot/list?pageNum=1&pageSize="+index;
         OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
                 Log.d(TAG, "onFailure: "+e.getMessage());
             }
             @Override
             public void onResponse(Call call, Response response) throws IOException {
                 if (index<23){
                     arrayList.clear();
                     idList.clear();
                     ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
                     layoutParams.height+=1690;
                     index+=3;
                     initView();
                 }else{
                     Looper.prepare();
                     Toast.makeText(stopCar.this, "我也是有底线的", Toast.LENGTH_SHORT).show();
                     Looper.loop();
                 }
             }
         });
         }
    public void seeStopMessage(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(stopCar.this,StopCarMessage.class);
                intent.putExtra("id",idList.get(position));
                startActivity(intent);
            }
        });
    }

}